package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyCatalog;
import cn.wolfcode.wolf2w.query.StrategyQuery;
import cn.wolfcode.wolf2w.repository.StrategyCatalogRepository;
import cn.wolfcode.wolf2w.repository.StrategyRepository;
import cn.wolfcode.wolf2w.service.IStrategyCatalogService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import cn.wolfcode.wolf2w.util.JsonResult;
import cn.wolfcode.wolf2w.util.UploadUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import sun.security.krb5.internal.crypto.Crc32CksumType;


import javax.servlet.annotation.MultipartConfig;
import java.util.List;
import java.util.Optional;

@Controller
@RequestMapping("strategy")
public class StrategyController {
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private StrategyRepository strategyRepository;
    @Autowired
    private IStrategyThemeService strategyThemeService;
    @Autowired
    private IStrategyCatalogService strategyCatalogService;

    @Autowired
    private StrategyCatalogRepository strategyCatalogRepository;


    @Autowired
    private MongoTemplate mongoTemplate;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo")StrategyQuery qo){
        //这里是springdata的分页
        Page<Strategy> page = strategyService.query(qo);
        model.addAttribute("page", page);
        return "strategy/list";//这里其实除了最初的加载的页面之外其他的所有的操作都是异步请求完成的
    }

    @RequestMapping("/saveOrUpdate")//保存或者修改
    @ResponseBody
    public Object saveOrUpdate(Strategy strategy){
        System.out.println(JSON.toJSON(strategy));
        strategyService.saveOrUpdate(strategy);
        return JsonResult.success();
    }
    @RequestMapping("/input")//保存或者修改
    public String input(Model model,String id){

        model.addAttribute("themes",strategyThemeService.list());
        model.addAttribute("catalogs",strategyCatalogService.getVo());
        return "strategy/input";
    }

    @RequestMapping("changeState")
    @ResponseBody
    public Object changeState(String id,int state){
        Optional<Strategy> strategy =  strategyRepository.findById(id);
        System.out.println(strategy.get().getState());
        strategy.get().setState(state==0?0:1);
        System.out.println(strategy.get().getState());
        strategyRepository.save(strategy.get());
        return JsonResult.success();
    }

}
